package top.yifengxiner.common.core.pojo;

import java.util.HashMap;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/4 18:50
 * @description：消息返回工具类
 * @modified By：
 * @version: 0.0.1$
 */
public class AjaxResult extends HashMap<String,Object>
{
    private static final long serialVersionUID = 1L;

    /*状态码*/
    public static final String CODE_TAG = "code";
    /*返回内容*/
    public static final String MSG_TAG = "msg";
    /*数据对象*/
    public static final String DATA_TAG = "data";

    /*
    状态码列表
     */
    public enum CodeType
    {
        /*成功状态码*/
        SUCCESS(200), /*警告状态码*/
        WARN(401), /*错误状态码*/
        ERROR(500);
        private final int value;

        CodeType(int value)
        {
            this.value = value;
        }

        public int getValue()
        {
            return this.value;
        }
    }

    /*初始化一个AjaxResult对象，表示一个空消息*/
    public AjaxResult()
    {

    }

    /*初始化AjaxResult对象*/
    public AjaxResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
    }

    /*链式调用*/
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }

    public String getMsg()
    {
        return (String) this.get(MSG_TAG);
    }

    /*成功返回消息*/
    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }

    /*成功返回消息，自定义数据*/
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    /*成功返回消息，自定义消息*/
    public static AjaxResult success(String msg)
    {
        return AjaxResult.success(msg, null);
    }

    /*成功返回消息，自定义消息和数据*/
    public static AjaxResult success(String msg, Object data)
    {
        return new AjaxResult(CodeType.SUCCESS.value, msg, data);
    }

    /*警告*/
    public static AjaxResult warn(String msg)
    {
        return AjaxResult.warn(msg, null);
    }

    /*警告*/
    public static AjaxResult warn(String msg, Object data)
    {
        return new AjaxResult(CodeType.WARN.value, msg, data);
    }

    /*错误*/
    public static AjaxResult error()
    {
        return AjaxResult.error("操作失败");
    }

    /*错误*/
    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    /*错误*/
    public static AjaxResult error(String msg, Object data)
    {
        return new AjaxResult(CodeType.ERROR.value, msg, data);
    }

    public AjaxResult setData(Object data)
    {
        this.replace(DATA_TAG,data);
        return this;
    }
}
